package org.netEase.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.netEase.dao.zhutiDao;
import org.netEase.dao.zhutizuDao;
import org.netEase.dao.impl.zhutiDaoImpl;
import org.netEase.dao.impl.zhutizuDaoImpl;
import org.netEase.entity.zhuti;
import org.netEase.entity.zhutizu;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

@WebServlet(urlPatterns = { "/backstageManager/zhutizuadd.shtml" })
public class zhutizuAdd extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	zhutiDao zhutiDao = new zhutiDaoImpl();
		List<zhuti> lists=zhutiDao.getlist();
		request.setAttribute("lists", lists);
		request.getRequestDispatcher("/backstageManager/product-category-add.jsp").forward(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	// TODO Auto-generated method stub
    	 SmartUpload smartUpload = new SmartUpload();
 		smartUpload.initialize(this, request, response);
 		try {
 			smartUpload.upload();
 		} catch (SmartUploadException e) {
 			e.printStackTrace();
 		}
 		Request req = smartUpload.getRequest();
    	String yid=req.getParameter("yid");
    	String zname=req.getParameter("zname");
    	String zimg=req.getParameter("zimg");
    	Files files = smartUpload.getFiles();
		File file = files.getFile(0);
		zhutizu zu=new zhutizu();
		zu.setYid(Integer.parseInt(yid));
		zu.setzName(zname);
		zu.setZimg(zimg);
		if(file.isMissing() == false){
			try {
				file.saveAs("./images/index/"+file.getFileName());
			} catch (SmartUploadException e) {
				e.printStackTrace();
			}
			
			zu.setZimg(file.getFileName());

		}
		zhutizuDao daoz=new zhutizuDaoImpl();
		int re=daoz.add(zu);
		if(re>0) {
			response.sendRedirect(request.getContextPath() + "/backstageManager/zhutizu.shtml");
		}
		
    }
}
